package com.muyu.danmo.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoPrintingMessage;
import com.muyu.danmo.service.IDanmoPrintingMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 印刷出版Controller
 *
 * @author yangle
 * @date 2024-10-15
 */
@RestController
@RequestMapping("/printingMessage")
public class DanmoPrintingMessageController extends BaseController
{
    @Autowired
    private IDanmoPrintingMessageService danmoPrintingMessageService;

    /**
     * 查询印刷出版列表
     */
//    @RequiresPermissions("system:printingMessage:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoPrintingMessage danmoPrintingMessage) {
        PageHelper.startPage(danmoPrintingMessage.getPageNum(), danmoPrintingMessage.getPageSize());
        List<DanmoPrintingMessage> list = danmoPrintingMessageService.selectDanmoPrintingMessageList(danmoPrintingMessage);
        PageInfo<DanmoPrintingMessage> danmoInformationPageInfo = new PageInfo<>(list);
        return Result.success(danmoInformationPageInfo);
    }

    /**
     * 导出印刷出版列表
     */
//    @RequiresPermissions("system:printingMessage:export")
    @Log(title = "印刷出版", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoPrintingMessage danmoPrintingMessage)
    {
        List<DanmoPrintingMessage> list = danmoPrintingMessageService.selectDanmoPrintingMessageList(danmoPrintingMessage);
        ExcelUtil<DanmoPrintingMessage> util = new ExcelUtil<DanmoPrintingMessage>(DanmoPrintingMessage.class);
        util.exportExcel(response, list, "印刷出版数据");
    }

    /**
     * 获取印刷出版详细信息
     */
//    @RequiresPermissions("system:printingMessage:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        return success(danmoPrintingMessageService.selectDanmoPrintingMessageById(id));
    }

    /**
     * 新增印刷出版
     */
//    @RequiresPermissions("system:printingMessage:add")
    @Log(title = "印刷出版", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public Result add(@RequestBody DanmoPrintingMessage danmoPrintingMessage)
    {
                LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return error("请登录");
        }
        return danmoPrintingMessageService.insertDanmoPrintingMessage(danmoPrintingMessage);
    }

    /**
     * 修改印刷出版
     */
//    @RequiresPermissions("system:printingMessage:edit")
    @Log(title = "印刷出版", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public Result edit(@RequestBody DanmoPrintingMessage danmoPrintingMessage)
    {
        return danmoPrintingMessageService.updateDanmoPrintingMessage(danmoPrintingMessage);
    }

    /**
     * 删除印刷出版
     */
//    @RequiresPermissions("system:printingMessage:remove")
    @Log(title = "印刷出版", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return danmoPrintingMessageService.deleteDanmoPrintingMessageByIds(ids);
    }

    /**
     * 查看自己详情
     */
    @GetMapping("/danmoPrintingMessageByUserId")
    public Result danmoPrintingMessageByUserId() {
        {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser==null){
                return error("请登录");
            }
            Long userId = loginUser.getUserid();
//        Long userId = Long.valueOf(1); // 固定的用户ID

            return  danmoPrintingMessageService.danmoPrintingMessageByUserId(userId);
        }
    }

    /**
     * 查看自己印刷
     */
    @GetMapping("/danmoPrintingMessageUserId")
    public Result<List<DanmoPrintingMessage>> danmoPrintingMessageUserId() {
        {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (loginUser==null){
                return error("请登录");
            }
            Long userId = loginUser.getUserid();
//        Long userId = Long.valueOf(1); // 固定的用户ID
            DanmoPrintingMessage danmoPrintingMessage = new DanmoPrintingMessage();
            danmoPrintingMessage.setUserId(userId);
            List<DanmoPrintingMessage> danmoPrintingMessages = danmoPrintingMessageService.selectDanmoPrintingMessageList(danmoPrintingMessage);
            return   Result.success(danmoPrintingMessages);
        }
    }
}
